package com.wbao.dianniu.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.GridView;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.R;

/**
 * 报名权限选择
 * 
 * @author yejianjun
 *
 */
public class AuthoritySelected extends BaseActivity {

	private GridView mGridView;
	String[] data_item = null;
	private int[] selectArgs;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.authirty_selected_dialog);
		setTitleName("报名权限");
		mGridView = (GridView) findViewById(R.id.authirty_selected_gridview);
		MyAdapter adapter = new MyAdapter(this);
		data_item = getResources().getStringArray(R.array.job_array);
		adapter.setData(data_item);
		mGridView.setAdapter(adapter);
		selectArgs = new int[data_item.length];
		
	}
	
	class MyAdapter extends BaseAdapter{

		private String[] mData;
		private Context _context;
		public MyAdapter(Context context) {
			if(null == mData){
				mData = new String[]{};
			}
			_context = context;
		}
		
		public void setData(String[] data){
			mData = data;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mData.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder 	holder = null;
			if(null == convertView){
				holder = new ViewHolder();
				convertView = LayoutInflater.from(_context).inflate(R.layout.authirty_gridview_item, null);
				holder.textview = (CheckedTextView) convertView.findViewById(R.id.gridview_checktext);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			holder.textview.setText(mData[position]);
			holder.textview.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if (v.isSelected()) {
						selectArgs[position] = 0;
					} else {
						selectArgs[position] = 1;
					}
					v.setSelected(!v.isSelected());
				}
			});
			return convertView;
		}
		
		class ViewHolder{
			CheckedTextView textview;
		}
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back_button:
			String ret = "";
			int counts = 0;
			for (int i = 0; i < selectArgs.length; i++) {
				if( 1 == selectArgs[i]){
					ret+=data_item[i]+",";
					counts++;
				}
			}
			if(counts> 0){
				ret = ret.substring(0, ret.length()-1);
			}
			Intent intent = new Intent();   
            //把返回数据存入Intent
            intent.putExtra(CreateActivity.RET_DATE, ret);
            //设置返回数据
            setResult(RESULT_OK, intent);
            this.finish();
			break;

		default:
			break;
		}
		
		
	}
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		}
		public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		}
	
}
